#include "SoapService.h"
#include "soapObject.h"
#include "coreremote.nsmap"

static QSoapService* serviceObject;

int coreremote__SendMouseEvent(struct soap* soap, int xpos, int ypos, int action, int pointID, char** response){
    serviceObject->soapLog(QString("xpos : %1").arg(xpos));
    serviceObject->soapLog(QString("ypos : %1").arg(ypos));
    serviceObject->soapLog(QString("action : %1").arg(action));
    serviceObject->soapLog(QString("pointID : %1").arg(pointID));
    *response = "<recvdata value=\'OK\'/>";
    return SOAP_OK;
}

int coreremote__SendKeyEvent(struct soap* soap, int keyCode, int action, char** response){
    serviceObject->soapLog(QString("keyCode : %1").arg(keyCode));
    serviceObject->soapLog(QString("action : %1").arg(action));
    *response = "11";
    return SOAP_OK;
}

int coreremote__RemoteCtrEvent(struct soap* soap, int eventType, int screenID, int actionID, char** response){
    serviceObject->soapLog(QString("eventType : %1").arg(eventType));
    serviceObject->soapLog(QString("screenID : %1").arg(screenID));
    serviceObject->soapLog(QString("actionID : %1").arg(actionID));
    *response = "12";
    return SOAP_OK;
}

int coreremote__GetRemoteCtrStatus(struct soap* soap, int eventType, int screenID, int actionID, char** response){
    serviceObject->soapLog(QString("eventType : %1").arg(eventType));
    serviceObject->soapLog(QString("screenID : %1").arg(screenID));
    serviceObject->soapLog(QString("actionID : %1").arg(actionID));
    *response = "result : SOAP_OK";

    return SOAP_OK;
}

void SoapService::threadStart(){
    start(TimeCriticalPriority);
}

void SoapService::run(){
    ServiceService soap;
    int ret;
    soap_init(&soap);

    if (!soap_valid_socket(soap.bind("127.0.0.1", 1563, 100)))
        soap_print_fault(&soap, stderr);
    else
    {
        serviceObject->soapLog(QString("Bind to port %1 successful").arg(1563));
        soap.accept_timeout = 3600;

        for (;;)
        {
            int sock = (int)soap.accept();
            if (!soap_valid_socket(sock))
            {
                if (soap.errnum)
                    soap_print_fault(&soap, stderr);
                else
                {
                    serviceObject->soapLog("Server timed out (see code how to change this)\n");
                    break;
                }
            }
            serviceObject->soapLog(
                        QString("Accepting socket %1 connection from IP %2.%3.%4.%5... ")
                        .arg(sock)
                        .arg((int)(soap.ip>>24)&0xFF)
                        .arg((int)(soap.ip>>16)&0xFF)
                        .arg((int)(soap.ip>>8)&0xFF)
                        .arg((int)soap.ip&0xFF));
            if (soap.serve())
                soap_print_fault(&soap, stderr);
            serviceObject->soapLog("done");
            soap_destroy(&soap);
            soap_end(&soap);
        }
    }

    ret = soap.error;
    soap_destroy(&soap);
    soap_end(&soap);
    soap_done(&soap);
}


QSoapService::QSoapService(QObject *parent) :
    QObject(parent)
{
    serviceObject = this;
    mSoapService = new SoapService;
    mSoapService->threadStart();
}

QSoapService::~QSoapService(){
    delete mSoapService;
}

void QSoapService::soapLog(QString log){
    emit serviceLog(log);
}
